package com.parkwalk.xxx.component.tab;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;

import com.parkwalk.xxx.component.AbstractPanelWithMouseEvent;
import com.parkwalk.xxx.util.Config;
import com.parkwalk.xxx.util.SwingUtil;

public class ToolbarButton extends AbstractPanelWithMouseEvent {

	private static final long serialVersionUID = -5151932169696310580L;
	private String name;
	private Image image;
	private int frameType = 0;
	public static int FRAME_RIGHT = 1;
	public static int FRAME_AROUND = 4;

	private int mbStatus = 0;
	private boolean clicked = false;

	private float alpha = 1f;

	public ToolbarButton(String name) {
		setName(name);
		setOpaque(false);
	}

	public void mouseEntered(MouseEvent e) {
		if (clicked)
			return;
		new Thread() {
			public void run() {
				float a = 0f;
				while (a <= 1f) {
					alpha = a;
					ToolbarButton.this.repaint();
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
					}
					a += 0.1;
				}
			}
		}.start();
	}

	public void mouseExited(MouseEvent e) {
	}

	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
		g2.setComposite(composite);

		if (this.isMouseEntered) {
			drawButtonBackground(g2, new Color(253, 236, 219), new Color(253, 223, 187), new Color(255, 206, 105),
					new Color(255, 255, 222));
		}
		if (clicked) {
			drawButtonBackground(g2, new Color(255, 199, 99), new Color(253, 236, 219), new Color(253, 236, 219),
					new Color(255, 199, 99));
		}
		composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
		g2.setComposite(composite);
		if (frameType == FRAME_AROUND) {
			g.setColor(Config.COLOR_TOOLBAR_FRAME);
			g.drawRoundRect(1, 1, getWidth() - 4, getHeight() - 2, 3, 3);
		} else if (frameType == FRAME_RIGHT) {
			g.setColor(Config.COLOR_TOOLBAR_FRAME);
			g.drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 2);
		}

		SwingUtil.setFractionalmetricsOn(g2);
		g.setColor(Config.COLOR_UI_TEXT);
		g.setFont(Config.FONT_UI_TEXT);

		if (getImage() != null) {
			g.drawImage(getImage(), (getWidth() - getImage().getWidth(this)) / 2, 5 + mbStatus, this);
			g.drawString(getName(), (getWidth() - getName().length() * Config.FONT_UI_TEXT.getSize()) / 2, getImage()
					.getHeight(this) + 30 + mbStatus);
		} else {
			g.drawString(getName(), (getWidth() - getName().length() * Config.FONT_UI_TEXT.getSize()) / 2,
					10 + mbStatus);
		}
	}

	public void drawButtonBackground(Graphics2D g2, Color c1, Color c2, Color c3, Color c4, Color frameColor) {
		g2.setColor(frameColor);
		g2.drawRoundRect(1, 1, this.getWidth() - 4, this.getHeight() - 2, 3, 3);

		g2.setPaint(new GradientPaint(2, 2, c1, 1, this.getHeight() / 3, c2));
		g2.fillRect(2, 2, this.getWidth() - 5, this.getHeight() / 3);
		g2.setPaint(new GradientPaint(1, this.getHeight() / 3, c3, 1, this.getHeight(), c4));
		g2.fillRect(2, this.getHeight() / 3, this.getWidth() - 5, this.getHeight() / 3 * 2 - 1);
	}

	public void drawButtonBackground(Graphics2D g2, Color c1, Color c2, Color c3, Color c4) {
		drawButtonBackground(g2, c1, c2, c3, c4, Config.COLOR_UI_FRAME);
	}

	public void pushButton(boolean x) {
		clicked = x;
	}

	public void mousePressed(MouseEvent e) {
		mbStatus = 1;
	}

	public void mouseReleased(MouseEvent e) {
		mbStatus = 0;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public int getFrameType() {
		return frameType;
	}

	public void setFrameType(int frameType) {
		this.frameType = frameType;
	}

}
